package com.l.controller.qita;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

public class QIta {
	// 将字符串3个一排输出
	public static List<String> threesys() {
		List<String> listStr = new ArrayList<String>();
		// 字符串
		String str = "从前有座山，山里有座庙，庙里有个缸，缸里有个锅，锅里有个盆，盆里有个碗，碗里有个勺，勺里有个针,针里有个小人国。";
		// 字符串总长度
		int len = str.length();
		// 总行数
		int lineNum = len % 3 == 0 ? len / 3 : len / 3 + 1;
		// 临时存放字符串变量
		String subStr = "";
		for (int i = 1; i <= lineNum; i++) {
			if (i < lineNum) {
				subStr = str.substring((i - 1) * 3, i * 3);
			} else {
				subStr = str.substring((i - 1) * 3, len);
			}
			// 添加分割字符串
			listStr.add(subStr);
		}
		return listStr;
	}

	// 获取今天以后的时间
	public static String dateToString(Date date, int random) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_YEAR, random);// 增加一天,负数为减少一天
		// calendar.add(Calendar.DAY_OF_MONTH, 1);//增加一天
		// calendar.add(Calendar.DATE,1);//增加一天
		// calendar.add(Calendar.WEEK_OF_MONTH, 1);//增加一个礼拜
		// calendar.add(Calendar.WEEK_OF_YEAR,1);//增加一个礼拜
		// calendar.add(Calendar.YEAR, 1);//把日期往后增加一年.整数往后推,负数往前移动
		date = calendar.getTime();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s = df.format(date);
		return s;
	}

	// 计算时间差
	public static String timecha(String time1, String time2) throws Exception {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d1 = df.parse(time1);
		Date d2 = df.parse(time2);
		long diff = d1.getTime() - d2.getTime();// 这样得到的差值是微秒级别
		long hours = diff / (1000 * 60 * 60);
		long minutes = (diff - hours * (1000 * 60 * 60)) / (1000 * 60);
		return hours + "小时" + minutes + "分钟";
	}

	// double四舍五入
	public static double siwu(double d) {
		double f = 0.2991111111;
		BigDecimal b = new BigDecimal(f);
		double f1 = b.setScale(2, RoundingMode.HALF_UP).doubleValue();
		return f1;
	}

	// 将字符串中出现次数最多的字符输出
	public static Map<Character, Integer> countzifu(String a) {
		char[] chars = a.toCharArray();
		Map<Character, Integer> map = new TreeMap<Character, Integer>();
		for (char c : chars) {
			if (map.containsKey(c)) {
				map.put(c, map.get(c) + 1);
			} else {
				map.put(c, 1);
			}
		}
		// 直接输出字符以及出现的个数
		for (Map.Entry<Character, Integer> entry : map.entrySet()) {
			System.out.println("字符是" + entry.getKey() + "出现的次数是"
					+ entry.getValue());
		}
		return map;
	}
	//排序map
    private static void sortMap(Map<Character, Integer> map) {
        List<Map.Entry<Character, Integer>> list = new ArrayList<Map.Entry<Character,Integer>>(map.entrySet());
        Collections.sort(list,new Comparator<Map.Entry<Character, Integer>>() {
            
            //倒序排列
            @Override
            public int compare(Entry<Character, Integer> o1,
                    Entry<Character, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        
        //打印出现自出的字符以及次数
        System.out.println("出现次数最多的字符"+list.get(0).getKey()+"==次数为"+list.get(0).getValue());
    }

	public static void main(String[] args) throws Exception {
		String str = "abccadbcadbc";
		// 字符串放入map
		Map<Character, Integer> map = countzifu(str);
		sortMap(map);
	}
}
